//
//  JSTLocation.m
//  PackDemo
//
//  Created by 01 on 16/11/2.
//  Copyright © 2016年 蔡正军. All rights reserved.
//

#import "JSTLocation.h"

static JSTLocation * manager = nil;

@implementation JSTLocation

// 多线程单例
+ (JSTLocation *)sharedInstace
{
    static JSTLocation *manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[JSTLocation alloc] init];
        manager.address =IsStrEmpty([[NSUserDefaults standardUserDefaults] objectForKey:@"address"]) ? @"深圳市" : [[NSUserDefaults standardUserDefaults] objectForKey:@"address"];
    });
    return manager;
}

- (instancetype)init
{
    self = [super init];
    if (self) {}
    return self;
}

- (void) requestAddress:(void (^)()) address
{
    [JSTServer requestAddressData:^(NSDictionary *addressData) {
        if ([[addressData valueForKey:@"status"] isEqualToString:@"1"]) {
            self.address = [NSString stringWithFormat:@"%@/%@", [addressData valueForKey:@"province"],[addressData valueForKey:@"city"]];
            NSString *recStr = [NSString stringWithFormat:@"%@", [addressData valueForKey:@"rectangle"]];
            if (!IsStrEmpty(recStr) && [recStr containsString:@";"]) {
                NSArray *jwData = [[addressData valueForKey:@"rectangle"] componentsSeparatedByString:@";"];
                NSMutableArray *jwArr = [NSMutableArray array];
                for (int i = 0; i < jwData.count; i++) {
                    NSArray *arr = [jwData[i] componentsSeparatedByString:@","];
                    for (int j = 0; j < arr.count; j++) {
                        [jwArr addObject:arr[j]];
                    }
                }
                self.longitude = [NSString stringWithFormat:@"%lf", ([jwArr[0] doubleValue] + [jwArr[2] doubleValue])/2.];
                self.latitude = [NSString stringWithFormat:@"%lf", ([jwArr[1] doubleValue] + [jwArr[3] doubleValue])/2.];
            }
            [JSTSystemInfo saveUserLongitude:self.longitude latitude:self.latitude];
            [[NSUserDefaults standardUserDefaults] setObject:self.address forKey:@"address"];
            DLog(@"------经度 = %@ 纬度 = %@\n -----省市区域 = %@", self.longitude, self.latitude, self.address);
        }
        address();
    }];
}



@end
